﻿using System;
using System.Text;

namespace Util.QueryParser.Token
{
    class LiteralCI : IToken      //Case insensitive string
    {
        private readonly string str;

        public LiteralCI(string str)
        {
            if (str.Length < 2 || str[0] != '"' || str[str.Length - 1] != '"' || str[str.Length - 2] == '\\')
            {
                throw new ArgumentException("Error in parameter str", "str");
            }
            this.str = str.Substring(1, str.Length-2);
        }

        public bool Evaluate(string value)
        {
            try
            {
                return System.Text.RegularExpressions.Regex.Match(value, str, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success;
            }
            catch (ArgumentException ex)
            {
                throw new ParserException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Couldn't evaluate({0}): {1}", str, value), ex);
            }
        }
    }
}
